<?php
/**
 *
 *
 */
namespace Yz\modules\Users;

class Controller_Login
    extends \Yz_Controller_Templated
    implements \Yz_Controller_Interface
{

    protected $loginFailure = false;
    protected $loginFailureCode = 0;



    /**
     *
     */
    public function checkAccess()
    {
        debug();

        $user = \Yz::selectLibrary('user');
        if ($user->isAuthenticated()) {
            throw new \Yz_Access_Exception("User '$user->login' can not login unless logged out");
        }

        return $this;
    }

    /**
     *
     */
    protected function doGET()
    {
        debug();

        // shortcut
        $m = $this->module;

        $model = $m->selectModel("List");
        $users = $model->fetchList();

        $template = $this->selectTemplate();

        $template->usersList = $users;
        $template->fromURL = $this->getRequest()->fetchArgument('path');

        $template->loginFailure = $this->loginFailure;

        // may be none
        $this->insertCaptchaBlock($template);

        return $this->outputTemplate($template);
    }


    /**
     *
     *
     */
    protected function doPOST()
    {
        debug();
        $this->loginFailure = true;

        if ( ! $this->testCaptcha() ) {
            $this->loginFailureCode = 16;
            return $this->doGET();
        }

        $user = \Yz::selectLibrary("user");

        $rq = $this->getRequest();
        $login = $rq->fetchPOSTArgument('login');
        $password = $rq->fetchPOSTArgument('password');
        try {
            $user->authenticate($login, $password);
            $this->loginFailure = false;
        } catch (\Yz_Exception_ItemNotFound $ex) {
            debug($ex);
            $this->loginFailureCode = 4;
        } catch (\Yz_User_Exception $ex) {
            debug($ex);
            $this->loginFailureCode = 8;
        } catch (\Yz_Exception_InvalidArgument $ex) {
            debug($ex);
            $this->loginFailureCode = 1;
        }
        if ($this->loginFailure) {
            return $this->doGET();
        }

        $url = $rq->fetchArgument("path", $this->router->getWebPrefix());

        $template = $this->selectTemplate("loginDone");
        $template->url = $url;
        $this->getView()
            ->setRedirect( $url )
            ;
        return $this->outputTemplate($template);
    }



    /**
     *
     *
     */
    protected function insertCaptchaBlock($template)
    {
        debug();

        if ($this->captchaEnabled()) {
            $captTemplate = $this->selectTemplate("captcha");
            $captTemplate->unique = YZ_TIME;
            $template->includeTemplate($captTemplate);
        }
        return $this;
    }

    /**
     *
     *
     */
    protected function testCaptcha()
    {
        debug();

        $res = true;
        if ($this->captchaEnabled()) {
            $captModule = \Yz::selectModule("captcha");
            $item = $captModule->selectModel("Captcha");
            $res = $item->verifyCode($this->getRequest()->fetchPOSTArgument("captcha"));
        }
        return $res;
    }

    protected function captchaEnabled()
    {
        debug();
        return \Yz::getConfig()->fetchBoolean("users.login.captcha.enabled");
    }

}
